package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.xz.stu.server.entity.Term;
import com.xz.stu.server.mapper.TermMapper;
import com.xz.stu.server.service.TermService;
import com.xz.stu.server.vo.term.TermCreateReqVO;
import com.xz.stu.server.vo.term.TermPageReqVO;
import com.xz.stu.server.vo.term.TermRespVO;
import com.xz.stu.server.vo.term.TermUpdateReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 熊磊
* @description 针对表【school_term(学期表)】的数据库操作Service实现
* @createDate 2025-02-26 15:34:32
*/
@Service
public class TermServiceImpl extends ServiceImpl<TermMapper, Term>
    implements TermService {

    @Override
    public Long createTerm(TermCreateReqVO createReqVO) {
        Term term = BeanUtils.toBean(createReqVO, Term.class);
        this.save(term);
        return term.getId();
    }

    @Override
    public void updateTerm(TermUpdateReqVO updateReqVO) {
        Term term = BeanUtils.toBean(updateReqVO, Term.class);
        this.updateById(term);
    }

    @Override
    public void deleteTerm(List<Long> id) {
        this.removeByIds(id);
    }

    @Override
    public TermRespVO getTerm(Long id) {
        Term term = this.getById(id);
        return BeanUtils.toBean(term, TermRespVO.class);
    }

    @Override
    public PageResult<TermRespVO> getTermPage(TermPageReqVO pageVO) {
        LambdaQueryWrapper<Term> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(pageVO.getStatus()),Term::getStatus, pageVO.getStatus())
                .eq(Objects.nonNull(pageVO.getTermCode()),Term::getTermCode, pageVO.getTermCode())
                .eq(Objects.nonNull(pageVO.getTermName()),Term::getTermName, pageVO.getTermName())
                .orderByDesc(Term::getStartDate);
        Page<Term> page = this.page(new Page<>(pageVO.getPageNo(), pageVO.getPageSize()), lambdaQueryWrapper);
        if (page != null) {
            return new PageResult<>(BeanUtils.toBean(page.getRecords(), TermRespVO.class), page.getTotal());
        }
        return null;
    }

    @Override
    public List<TermRespVO> getListSimpleTerm() {
        List<Term> terms = this.lambdaQuery().eq(Term::getStatus, 0).orderByDesc(Term::getStartDate).list();
        if (terms != null) {
            return BeanUtils.toBean(terms, TermRespVO.class);
        }
        return null;
    }
}




